package android.se1;
import android.preference.PreferenceManager;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.os.StrictMode;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MavBulletinActivity extends Activity {
	EditText email, pass;
	TextView error;
	Button signin;
		
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork() // or	.detectAll() for all detectable problems
				.penaltyLog().build());
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		email = (EditText) findViewById(R.id.editText1);
		pass = (EditText) findViewById(R.id.editText2);
		signin = (Button) findViewById(R.id.signin);
		error = (TextView) findViewById(R.id.error1);
		Button signup = (Button) findViewById(R.id.signup);
		Button forgot = (Button) findViewById(R.id.forgotpassword);
		
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

		// Listening to button event
		signup.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// Starting a new Intent
				Intent signupScreen = new Intent(getApplicationContext(),
						SecondActivity.class);
				startActivity(signupScreen);
			}
		});		
		// Listening to button event
		forgot.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v1) {
				// Starting a new Intent
				Intent forgotScreen = new Intent(getApplicationContext(),
						ForgotPasswordActivity.class);
				startActivity(forgotScreen);

			}
		});
		signin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub

				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

				String mavs = "@mavs.uta.edu";
				String email_address = email.getText().toString();
				int check;
				check = email_address.indexOf(mavs);

				if (check == -1) {
					//error.setText("Invalid Email Address! Please Type Valid MAVS Email Address.");
					alertDialog.setTitle("Sign In");
					alertDialog
							.setMessage("Invalid Email Address! Please Type Valid MAVS Email Address.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
					
				} else {
					postParameters.add(new BasicNameValuePair("emailaddress",
							email.getText().toString()));

					postParameters.add(new BasicNameValuePair("password", pass
							.getText().toString()));
			
					String response = null;

					try {

						response = CustomHttpClient.executeHttpPost(
								//"http://192.168.1.145/CSE5324/signin.php",
								//"http://129.107.149.180/CSE5324/signin.php",
								"http://omega.uta.edu/~nxk0518/CSE5324/signin.php",
								//"http://192.168.0.3/mavbulletin/signin.php",
								postParameters);

						String result = response.toString();

						result = result.replaceAll("\\s+", "");

						if (result.equals("1")) {
							System.out.println("Result =" + result);
							//error.setText("Sign In Successful");
							Toast.makeText(MavBulletinActivity.this,"Sign In Successful.", Toast.LENGTH_LONG).show();
							/*
							alertDialog.setTitle("Sign In");
							alertDialog
									.setMessage("Sign In Successful.");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int which) {
											// here you can add functions
										}
									});
							alertDialog.show();
							*/							
							SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
							SharedPreferences.Editor editor = sharedPref.edit();
							editor.putString("email",email_address);
							editor.commit();
							
							Intent viewadsScreen = new Intent(getApplicationContext(),
									ThirdActivity.class);
							startActivity(viewadsScreen);
						} else {
							System.out.println("Result =" + result);
							//error.setText("Sorry!! Incorrect Email Address or Password");
							alertDialog.setTitle("Sign In");
							alertDialog
									.setMessage("Sorry!! Incorrect Email Address or Password.");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int type) {
											// here you can add functions
										}
									});
							alertDialog.show();
						}
					} catch (Exception e) {
						Log.e("log_tag",
								"Error in http connection!!" + e.toString());
						//email.setText(e.toString());

					}

				}

			}

		});

	}

}